package com.example.ui.learn;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.example.huashangapp.R;
import com.example.net.AnalyzingJson;
import com.example.net.NetRequest;
import com.example.ui.learn.util.Detail;
import com.example.ui.learn.util.DetailAdapter;
import com.google.gson.JsonObject;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class DetailActivity extends Activity {
	private String id;
	private int type;
	private Button ra_topleft, ra_topright;
	private TextView ra_toptitle;
	private String theme, made, address, time, price; 
	private TextView tvtheme, tvmade, tvaddress, tvtime, tvprice;
	private TextView tvmade1;
	private String data;
	private ArrayList<Detail> da_list;
	private ListView da_lv;
	private DetailAdapter da_adapter;
	private String terId;//图片的id
    
	AlertDialog infoad;
	AlertDialog.Builder bulider;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.a_detail);
		da_list = new ArrayList<Detail>();
		Intent i = getIntent();
		type = i.getIntExtra("type", 0);
		
		id = i.getStringExtra("id");
		terId=i.getStringExtra("bId");
		ra_toptitle = (TextView) findViewById(R.id.layout_title_middle_text);
		switch (type) {
		case 1:
			ra_toptitle.setText("活动详情");
			break;
		case 0:
			ra_toptitle.setText("课程详情");
			break;

		default:
			break;
		}
		bulider = new AlertDialog.Builder(DetailActivity.this);
		bulider.setView(View.inflate(DetailActivity.this, R.layout.onloading, null))
				.create();
		infoad = bulider.show();
		new Thread(da_run).start();

		init();

	}

	Handler h = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case 1:
				infoad.dismiss();
				setText(type);
				da_adapter.setCcInfos(da_list);
				da_adapter.notifyDataSetChanged();
				break;
			case 2:
				infoad.dismiss();
				
			default:
				break;
			}

		};
	};

	private Runnable da_run = new Runnable() {
		@Override
		public void run() {
			data = NetRequest.Request_RecommendCourse(type,id, terId);
			try {
				JSONObject j = new JSONObject(data);

				String result2 = j.getString("response2");
				
				if (result2.equals("true")) {
					String result = j.getString("response");
					String result1 = j.getString("response1");
					getResponse(result);
					
					da_list = AnalyzingJson.getcontent_detail(result1);
					h.sendEmptyMessage(1);
				} else {
					
					h.sendEmptyMessage(2);
				}
			} catch (Exception e) {

			}
		}
	};

	private void setText(int num) {
		tvtheme.setText(theme);
		if (num == 1) {
			tvmade.setText(made);
			tvmade1.setText("主办:");
		} else {
			tvmade.setText(made);
			tvmade1.setText("主讲:");
		}
		tvaddress.setText(address);
		tvtime.setText(time);
		tvprice.setText(price);
	}

	private void getResponse(String data) {

		try {

			JSONArray ja = new JSONArray(data);
			JSONObject jo = ja.getJSONObject(0);
			theme = jo.getString("title");
			made = jo.getString("Zhuban");
			address = jo.getString("areas");
			time = jo.getString("dateAdded") + "开始\n" + jo.getString("dateEnd")+"结束";
			price = jo.getString("yuanjia");
		} catch (JSONException e) {
			
			e.printStackTrace();
		}

	}

	private void init() {
		ra_topleft = (Button) findViewById(R.id.layout_title_left_btn);
		ra_topright = (Button) findViewById(R.id.layout_title_right_btn);
		ra_topleft.setText("返回");
		ra_topright.setText("报名");

		tvtheme = (TextView) findViewById(R.id.ad_theme);
		tvmade = (TextView) findViewById(R.id.ad_made);
		tvaddress = (TextView) findViewById(R.id.ad_address);
		tvtime = (TextView) findViewById(R.id.ad_time);
		tvmade1 = (TextView) findViewById(R.id.ad_made1);
		tvprice = (TextView) findViewById(R.id.ad_price);
		da_lv = (ListView) findViewById(R.id.ad_lv);
		da_adapter = new DetailAdapter(this, da_list);
		da_lv.setAdapter(da_adapter);

		ra_topleft.setOnClickListener(ra_ocl);
		ra_topright.setOnClickListener(ra_ocl);
	}

	private OnClickListener ra_ocl = new OnClickListener() {

		@Override
		public void onClick(View v) {
			
			switch (v.getId()) {

			case R.id.layout_title_left_btn:
				finish();
				break;
			case R.id.layout_title_right_btn:

				break;
			default:
				break;
			}
		}
	};

}
